package com.zhxh.nzpos.setting;

import java.util.ArrayList;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.zhxh.nzpos.R;
import com.zhxh.nzpos.goods.GoodsItem;
import com.zhxh.nzpos.sell.ItemDeleteCallback;

public class PriceSettingAdapter extends BaseAdapter {

	private ArrayList<GoodsItem> items;
	private LayoutInflater inflater;
	private ItemActionCallback actionCallback;
	public ViewHolder holder;

	public PriceSettingAdapter(LayoutInflater inflater,
			ArrayList<GoodsItem> dealItems, ItemActionCallback actionCallback) {
		this.items = dealItems;
		this.inflater = inflater;
		this.actionCallback = actionCallback;
	}

	@Override
	public int getCount() {
		return items.size();
	}

	@Override
	public Object getItem(int position) {
		return items.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.price_setting_list_item,
					null);
			holder = new ViewHolder();
			holder.identifier = (TextView) convertView
					.findViewById(R.id.goods_identifier_et);
			holder.name = (TextView) convertView
					.findViewById(R.id.goods_name_et);
			holder.specification = (TextView) convertView
					.findViewById(R.id.goods_specification_et);
			holder.price = (TextView) convertView
					.findViewById(R.id.goods_price_et);
			holder.price2 = (TextView) convertView
					.findViewById(R.id.goods_price2_et);
			holder.saveBtn = (Button) convertView
					.findViewById(R.id.goods_update_btn);
			holder.saveBtn.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					if (actionCallback != null) {
						actionCallback.onUpdate(position, holder);
					}
				}
			});
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		GoodsItem item = items.get(position);
		if (item != null) {
			holder.identifier.setText(item.getId());
			holder.name.setText(item.getName());
			holder.specification.setText("" + item.getSpecification());
			holder.price.setText("" + item.getPrice1());

			holder.price2.setText("" + item.getPrice2());
		}
		return convertView;
	}

	public static class ViewHolder {
		TextView identifier;
		TextView name;
		TextView specification;
		TextView category;
		TextView price;
		TextView unit;
		TextView unit2;
		TextView price2;
		// TextView code;
		TextView points;
		Button saveBtn;
	}
}
